package com.imooc.demo.Controller;

import com.imooc.demo.Model.UsersVO;
import com.imooc.demo.Model.users;
import com.imooc.demo.Service.UserService;
import com.imooc.demo.Util.IMoocJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.nio.ch.IOUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


@RestController
@RequestMapping("/user")
@Api(value = "用户相关业务", tags = "用户相关业务的接口")
class UserController {

    @Autowired
    private UserService userService;


    @ApiOperation(value = "用户头像上传", notes = "文件上传接口")
    @ApiImplicitParam(name = "userId",value = "用户Id",required = true,dataType = "String",paramType = "query")
    @PostMapping("/uploadface")
    public IMoocJSONResult uploadFile(Integer userId, @RequestParam("file") MultipartFile[] files)throws Exception {
        if (userId.equals(null)){//StringUtils.isBlank(userId)
            return IMoocJSONResult.errorMsg("用户id不能为空");
        }
        //文保存的明明空间
        String fileSpace = "G:/ij_program1";
        //保存奥数据库中的相对路径
        String uploadPathDB = "/" + userId + "/face";
        FileOutputStream fileOutputStream = null;
        InputStream inputStream = null;
        try{
            if (files != null && files.length > 0) {
                String fileName = files[0].getOriginalFilename();
                if (StringUtils.isNoneBlank(fileName)) {
                    //文件上传的最终保存路径
                    String finalFacePath = fileSpace + uploadPathDB + "/" + fileName;
                    uploadPathDB += ("/" + fileName);
                    File outFile = new File(finalFacePath);
                    if (outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()) {
                        //创建父文件夹
                        outFile.getParentFile().mkdirs();
                    }
                    fileOutputStream = new FileOutputStream(outFile);
                    inputStream = files[0].getInputStream();
                    IOUtils.copy(inputStream, fileOutputStream);
                }
            }else {
                return IMoocJSONResult.errorMsg("上传出错");
            }
        }catch (Exception e){
            e.printStackTrace();
            return IMoocJSONResult.errorMsg("上传出错");
        }finally {
            if (fileOutputStream!=null){
                fileOutputStream.flush();
                fileOutputStream.close();
            }
        }
        users user=new users();
//        Integer Id=Integer.valueOf(userId);
        user.setId(userId);
        user.setFace_image(uploadPathDB);
        userService.updateUser(user);
        return IMoocJSONResult.ok(uploadPathDB);
    }


    @ApiOperation(value = "查询用户信息", notes = "查询用户信息的接口")
    @ApiImplicitParam(name = "userId",value = "用户Id",required = true,dataType = "String",paramType = "query")
    @PostMapping("/query")
    public IMoocJSONResult query(Integer userId)throws Exception {
        if(userId==null) {
            return IMoocJSONResult.errorMsg("用户账号不能为空！");
        }
        users userInfo=userService.queryUserInfo(userId);
        UsersVO userVO=new UsersVO();
        BeanUtils.copyProperties(userInfo,userVO);
        return IMoocJSONResult.ok(userVO);
//        users users = userService.queryUserInfo(userId);
//        UsersVO usersVo = new UsersVO();
//        BeanUtils.copyProperties(users,usersVo);
//        //userId为视频的发布者而fansId为此时的登录用户
//        usersVo.setIsfollow(userService.isFollow(fansId,userId));
//        return JSONResult.ok(usersVo);
    }

//    @ApiOperation(value = "显示用户是否喜欢",notes = "显示喜欢接口")
//    @PostMapping("/likevideo")
//    public JSONResult isLikeVideo(String loginId, String publishId, String videoId) {
//        if (StringUtils.isBlank(loginId)) {
//            return JSONResult.errorMsg("请重新登录");
//        }
//        Users user = userService.queryUserInfo(publishId);
//        UsersVo usersVo = new UsersVo();
//        BeanUtils.copyProperties(user,usersVo);
//        boolean islike = userService.isLikeVideo(loginId,videoId);
//
//        PublishVoByLike publishVoByLike = new PublishVoByLike();
//        publishVoByLike.setUsersVo(usersVo);
//        publishVoByLike.setIslike(islike);
//        return JSONResult.ok(publishVoByLike);
//    }
//
//    @ApiOperation(value = "关注视频", notes = "关注视频接口")
//    @PostMapping("/fans")
//    public JSONResult becameUserFans(String userId, String fansId) {
//        if (StringUtils.isBlank(userId) || StringUtils.isBlank(fansId)) {
//            return JSONResult.errorMsg("请重新登录");
//        }
//        userService.becameFans(userId,fansId);
//        return JSONResult.ok();
//    }
//
//    @ApiOperation(value = "取消关注", notes = "取消关注接口")
//    @PostMapping("/unfans")
//    public JSONResult cancelAttention(String userId, String fansId) {
//        if (StringUtils.isBlank(userId) || StringUtils.isBlank(fansId)) {
//            return JSONResult.errorMsg("请重新登录");
//        }
//        userService.cancelAttention(userId,fansId);
//        return JSONResult.ok();
//    }
}

